package com.ke.forum.forumapi.controller;

import com.ke.forum.common.common.Result;
import com.ke.forum.forumapi.service.UploadService;

import com.ke.forum.mbg.dao.UserMapper;
import com.ke.forum.mbg.entity.User;
import com.ke.forum.mbg.entity.dto.UserDto;
import com.ke.forum.mbg.mapstruct.UserMapstruct;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Qualifier("MinioService")
    @Autowired
    UploadService minioService;
    @Qualifier("OssService")
    @Autowired
    UploadService ossService;
    @Autowired
    UserMapper userMapper;

    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestParam(value = "file") MultipartFile file){
        try {
            String url = ossService.upload(file);
            HashMap<String, String> urlMap = new HashMap<>();
            urlMap.put("url",url);
            return Result.SUCCESS(urlMap);
        } catch (Exception e) {
            return Result.FAIL("上传失败");
        }
    }

    @PostMapping("/uploadBackground")
    public Result uploadBackground(@RequestParam(value = "file") MultipartFile file){
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        String url = null;
        try {
            url = ossService.upload(file);
        } catch (Exception e) {
            return Result.FAIL("上传失败");
        }
        String background = userDto.getBackground();
        if (null!=background&&!"".equals(background)){
            ossService.delete(background);
        }
        userDto.setBackground(url);
        User user = UserMapstruct.INSTANCE.toEntity(userDto);
        userMapper.updateById(user);
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("url",url);
        return Result.SUCCESS(urlMap);
    }

    @PostMapping("/uploadPortrait")
    public Result uploadPortrait(@RequestParam(value = "file") MultipartFile file){
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        String url = null;
        try {
            url = ossService.upload(file);
        } catch (Exception e) {
            return Result.FAIL("上传失败");
        }
        String portrait = userDto.getPortrait();
        if (null!=portrait&& !"".equals(portrait)){
            ossService.delete(portrait);
        }
        userDto.setPortrait(url);
        User user = UserMapstruct.INSTANCE.toEntity(userDto);
        userMapper.updateById(user);
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("url",url);
        return Result.SUCCESS(urlMap);
    }
}
